package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author zhufeng
 * @version 1.0
 * @date 2022-1-20 18:25
 */
public class 轮转数组_189 {
    public void rotate(int[] nums, int k) {
        int length = nums.length;
        boolean[] mark = new boolean[length];
        for (int i = 0; i < length; i++) {
            if(mark[i]) {
                continue;
            }
            int oldi = i;
            int tempOld = nums[i];
            int tempNew;
            do {
                mark[oldi] = true;
                int newi = (oldi+k) % length;
                tempNew = nums[newi];
                nums[newi] = tempOld;
                tempOld = tempNew;
                oldi = newi;
            } while (oldi != i);
        }
    }

    @Test
    public void testRotate () {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int k = 4;
        rotate(nums, k);
        System.out.println(Arrays.toString(nums));
    }
}
